package edu.com.wjt219971332.controller;

import edu.com.wjt219971332.entity.*;
import edu.com.wjt219971332.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("audiencevote")
public class AudiencevoteController {
    @Autowired
    private SingerService singerService;
    @Autowired
    private UserService userService;

    @Autowired
    private CompetitionService competitionService;

    @Autowired
    private AudiencevoteService audiencevoteService;
    @Autowired
    private MatchupService matchupService;

    @RequestMapping("/insert/{singerid}/{userid}/{competitionid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> audiencevote(@PathVariable("singerid") Integer singerid,
                                                          @PathVariable("userid") Integer userid,
                                                          @PathVariable("competitionid") Integer competitionid){
        Map<String, Object> response = new HashMap<>();
        Audiencevote audiencevote = new Audiencevote();
        audiencevote.setSingerid(singerid);
        audiencevote.setCompetitionid(competitionid);
        audiencevote.setUserid(userid);
        User user = userService.queryById(userid);
       Audiencevote audiencevote1 = audiencevoteService.weiyi(userid, competitionid);
       if(audiencevote1 == null && user.getRole().equals("1")){
            audiencevoteService.insert(audiencevote);
            response.put("message", "投票成功");
       }
       else if(user.getRole().equals("0")){
           response.put("message", "管理员不能参与投票");
       }
       else if(audiencevote1 != null && user.getRole().equals("1")){
           Singer singer = singerService.queryById(audiencevote1.getSingerid()) ;
           response.put("singername", singer.getName());
           response.put("message", "你已经投过票了");
       }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/vote/{competitionid}/{asingerid}/{bsingerid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> delete(@PathVariable("asingerid") Integer asingerid,
                                                    @PathVariable("bsingerid") Integer bsingerid,
                                                    @PathVariable("competitionid") Integer competitionid){
        Map<String, Object> response = new HashMap<>();
        int avote = audiencevoteService.countvote(competitionid, asingerid);
        response.put("avote", avote);
        int bvote = audiencevoteService.countvote(competitionid, bsingerid);
        response.put("bvote", bvote);
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/delete/{userid}/{competitionid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> vote(@PathVariable("userid") Integer userid,
                                                    @PathVariable("competitionid") Integer competitionid){
        Map<String, Object> response = new HashMap<>();
        Audiencevote audiencevote1 = audiencevoteService.weiyi(userid, competitionid);
        if(audiencevote1 == null){
            response.put("message", "还没投票不能取消,请先投票");
        }
        else{
            audiencevoteService.deleteByuserid(userid, competitionid);
            response.put("message", "取消成功");
        }
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/ajudge/{competitionid}")
    public String ajudge1(@PathVariable("competitionid") Integer competitionid, Model model) {
        model.addAttribute("competitionid", competitionid);
        return "audiencevote/ajudge";
    }
    @RequestMapping("/ajudge")
    public ResponseEntity<Map<String, Object>> ajudge(@RequestParam("competitionid") Integer competitionid,
                                                     @RequestParam("score") int score){
        Map<String, Object> response = new HashMap<>();
        Competition competition = competitionService.queryById(competitionid);
        competition.setAjudge(score);
        response.put("singername", competition.getAsinger());
        if(score > 100){
            response.put("message","打分不能超过100");
            return ResponseEntity.ok(response);
        }
        competitionService.update(competition);

        response.put("message", "打分成功");
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/bjudge/{competitionid}")
    public String bjudge1(@PathVariable("competitionid") Integer competitionid, Model model) {
        model.addAttribute("competitionid", competitionid);
        return "audiencevote/bjudge";
    }
    @RequestMapping("/bjudge")
    public ResponseEntity<Map<String, Object>> bjudge(@RequestParam("competitionid") Integer competitionid,
                                                      @RequestParam("score") int score){
        Map<String, Object> response = new HashMap<>();
        Competition competition = competitionService.queryById(competitionid);
        competition.setBjudge(score);
        response.put("singername", competition.getBsinger());
        if(score > 100){
            response.put("message", "打分不能超过100");
            return ResponseEntity.ok(response);
        }
        competitionService.update(competition);
        response.put("message", "打分成功");

        return ResponseEntity.ok(response);
    }
    @RequestMapping("/judge/{competitionid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> judge(@PathVariable("competitionid") Integer competitionid){
        Map<String, Object> response = new HashMap<>();
        Competition competition = competitionService.queryById(competitionid);
        int ajudge = competition.getAjudge();
        int bjudge = competition.getBjudge();
        response.put("ajudge", ajudge);
        response.put("bjudge", bjudge);
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/ascore/{competitionid}/{asingerid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> ascore(@PathVariable("competitionid") Integer competitionid,
                                                      @PathVariable("asingerid") Integer asingerid) {
        Map<String, Object> response = new HashMap<>();
        int avote = audiencevoteService.countvote(competitionid, asingerid);
        Competition competition = competitionService.queryById(competitionid);
        int ajudge = competition.getAjudge();
        int ascore = (avote + ajudge) / 2;
        competition.setAscore(ascore);
        competitionService.update(competition);
        response.put("ascore", ascore);
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/bscore/{competitionid}/{bsingerid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> bscore(@PathVariable("competitionid") Integer competitionid,
                                                      @PathVariable("bsingerid") Integer bsingerid) {
        Map<String, Object> response = new HashMap<>();
        int bvote = audiencevoteService.countvote(competitionid, bsingerid);
        Competition competition = competitionService.queryById(competitionid);
        int bjudge = competition.getBjudge();
        int bscore = (bvote + bjudge) / 2;
        response.put("bscore", bscore);
        competition.setBscore(bscore);
        competitionService.update(competition);
        return ResponseEntity.ok(response);
    }
    @RequestMapping("/end/{competitionid}/{asingerid}/{bsingerid}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> end(@PathVariable("competitionid") Integer competitionid,
                                                   @PathVariable("bsingerid") Integer bsingerid,
                                                   @PathVariable("asingerid") Integer asingerid) {
        Map<String, Object> response = new HashMap<>();
        Matchup matchup = new Matchup();
        Singer asinger = singerService.queryById(asingerid);
        Singer bsinger = singerService.queryById(bsingerid);
        Competition competition = competitionService.queryById(competitionid);
        int ascore = competition.getAscore();
        int bscore = competition.getBscore();
        matchup.setCompetitionid(competition.getCompetitionid());
        matchup.setAsinger(asinger.getSingerid());
        matchup.setBsinger(bsinger.getSingerid());
        if(ascore > bscore){
            matchup.setAsingerstatus("win");
            matchup.setBsingerstatus("loser");
        }
        else if(bscore > ascore){
            matchup.setBsingerstatus("win");
            matchup.setAsingerstatus("loser");
        }
        else {
            matchup.setAsingerstatus("equality");
            matchup.setBsingerstatus("equality");
        }
        matchup.setAsingername(asinger.getName());
        matchup.setBsingername(bsinger.getName());
        matchupService.insert(matchup);
        competition.setStatus(1);
        int avote = audiencevoteService.countvote(competitionid, asingerid);
        int bvote = audiencevoteService.countvote(competitionid, bsingerid);
        competition.setAuser(avote);
        competition.setBuser(bvote);
        competitionService.update(competition);
        return ResponseEntity.ok(response);
    }
}
